<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门票名称" prop="scenicName">
        <el-input
          v-model="queryParams.scenicName"
          placeholder="请输入门票名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据状态" prop="scenicStatus">
        <el-select
          v-model="queryParams.scenicStatus"
          placeholder="数据状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.ticket_scenic_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-hasPermi="['system:scenic:query']">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ticket:scenic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ticket:scenic:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ticket:scenic:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tictket:scenic:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scenicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="景区门票ID" align="center" prop="scenicId" /> -->
      <el-table-column label="门票名称" align="center" prop="scenicName" />
      <el-table-column label="联系电话" align="center" prop="scenicPhone" width="130"/>
      <el-table-column label="有效时间开始" align="center" prop="effectiveTimeStart" width="180">
      </el-table-column>
      <el-table-column label="有效时间结束" align="center" prop="effectiveTimeEnd" width="180">
      </el-table-column>
      <el-table-column label="标签" align="center" prop="scenicLabel" />
      <el-table-column label="限购数量" align="center" prop="limitPurchaseNum" />
      <el-table-column label="状态" align="center" prop="scenicStatus" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.ticket_scenic_status" :value="scope.row.scenicStatus"/>
          </template>
      </el-table-column>
      <!-- <el-table-column label="其他基础信息" align="center" prop="ticOtherId" /> -->
      <el-table-column label="认证方式" align="center" prop="scenicAuth" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_auth" :value="scope.row.scenicAuth"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding" min-width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ticket:scenic:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.scenicStatus==1"
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handleUpload(scope.row)"
            v-hasPermi="['ticket:scenic:onshelf']"
          >上架</el-button>
          <el-button
            v-if="scope.row.scenicStatus==2"
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            v-hasPermi="['ticket:scenic:offshelf']"
          >下架</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ticket:scenic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改景区门票基础对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="dialogIsCloseOnModal" :close-on-press-escape="dialogIsCloseOnModal">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- 景区信息 -->
        <el-divider content-position="left">景区信息</el-divider>
        <el-row>
          <el-col :span="24">
            <el-col :span="11">
              <el-form-item label="景区" prop="ticOtherId">
                  <el-select
                    v-model="form.ticOtherId"
                    placeholder="请选择景区"
                    clearable
                    style="width: 240px"
                    @change='changeScenicSelect'
                    :disabled='!isEdit'
                  >
                    <el-option
                      v-for="dict in dictScenicList"
                      :key="dict.ticOtherId"
                      :label="dict.name"
                      :value="dict.ticOtherId"
                    />
                  </el-select>
              </el-form-item>
            </el-col>
          </el-col>
          <!-- 选择景区后展示详细信息 -->
          <el-col :span="24" v-if='form.ticOtherId'>
            <el-col :span="11">
              <el-form-item label="星级" prop="starDetail">
                <el-input v-model="dictScenicListSelected.starDetail" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开放时间" >
                <el-input v-if="dictScenicListSelected.openTimeStart" :value="`${dictScenicListSelected.openTimeStart}-${dictScenicListSelected.openTimeEnd}`" disabled/>
                <el-input v-else value="" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="景点信息">
                <editor v-model="dictScenicListSelected.introduceDetails" :readOnly='false' :min-height="100"/>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="游客须知">
                <editor v-model="dictScenicListSelected.touristNotice" :readOnly='false' :min-height="100"/>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="地理位置" >
                <el-input v-model="dictScenicListSelected.addressDetails" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="坐标点" prop="starDetail">
                <el-input v-if="dictScenicListSelected.latitude &&dictScenicListSelected.longitude" :value="`${dictScenicListSelected.latitude}-${dictScenicListSelected.longitude}`" disabled/>
                <el-input v-else value="" disabled/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
        <!-- 门票信息 -->
        <el-divider content-position="left">门票信息</el-divider>
        <el-row>
          <el-col :span="11">
            <el-form-item label="门票名称" prop="scenicName">
              <el-input v-model="form.scenicName" placeholder="请输入门票名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="scenicPhone">
              <el-input v-model="form.scenicPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="有效时间" prop="effectiveTime">
              <el-date-picker
                v-model="effectiveTime"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期" @change='setEffectiveTime($event)'>
              </el-date-picker>
            </el-form-item>
          </el-col>
        <el-col :span="11">
            <el-form-item label="标签" prop="scenicLabel">
              <el-input v-model="form.scenicLabel" placeholder="请输入标签" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="限购数量" prop="limitPurchaseNum">
              <el-input-number v-model="form.limitPurchaseNum" placeholder="请输入限购数量" :min="0"  controls-position="right"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="11">
            <el-form-item label="数据状态" prop="scenicStatus">
                <el-select
                  v-model="form.scenicStatus"
                  placeholder="数据状态"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.ticket_scenic_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="11">
            <el-form-item label="认证方式" prop="scenicAuth">
              <el-select
                  v-model="form.scenicAuth"
                  placeholder="认证方式"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.sys_user_auth"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScenic, getScenic, delScenic, addScenic, updateScenic,getListTicOherBase ,getOnshelf,getOffshelf} from "@/api/ticket/scenic";

export default {
  name: "Scenic",
  dicts: ["ticket_scenic_status", "sys_user_auth"],
  data() {
    return {
      // 新增/编辑日期时间范围
      effectiveTime: null,
      // 选项景区list
      dictScenicList:[],
      dictScenicListSelected:'',
      // 判断新增/编辑
      isEdit:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 景区门票基础表格数据
      scenicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scenicName: null,
        scenicPhone: null,
        effectiveTimeStart: null,
        effectiveTimeEnd: null,
        scenicLabel: null,
        limitPurchaseNum: null,
        scenicStatus: null,
        isDel: null,
        ticOtherId: null,
        scenicAuth: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scenicPhone: [
          { pattern: /^((0\d{2,3}-?\d{7,8})|(1[3456789]\d{9}))$/,message: "请输入正确的联系电话",trigger: "blur"},
          { required: true, message: "请输入手机号", trigger: "blur",}
        ],
        scenicName:[
          { required: true,message: "请输入门票名称",trigger: "blur",},
        ],
        ticOtherId:[
          { required: true, message: "请选择景区",trigger: "change",},
        ],
        effectiveTime:[
          { required: true, validator: this.checkUserName, trigger: 'blur' },
        ],
        scenicLabel:[
          { required: true,message: "请输入标签",trigger: "blur",},
        ],
        limitPurchaseNum:[
          { required: true, message: "请输入限购数量", trigger: "blur",},
        ],
        scenicAuth:[
          { required: true, message: "请选择认证方式", trigger: "change"},
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTicOherBase();
  },
  methods: {
    checkUserName(rule, value, callback){
        value = this.effectiveTime;
        if (!value)
          callback(new Error("请选择日期"));
        else
          callback();
      },
    /** 查询景区门票基础列表 */
    getList() {
      this.loading = true;
      listScenic(this.queryParams).then(response => {
        this.scenicList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(err=>{
        console.log(err)
        this.loading=true
      });
    },
    // 获取景点信息
    getTicOherBase(){
      getListTicOherBase().then(res=>{
        this.dictScenicList=res.data
      })
    },
    // 选择景点
    changeScenicSelect(selected){
      this.dictScenicList.map(item=>{
        if(item.ticOtherId==selected){
          this.dictScenicListSelected=item
        }
      })
    },
     // 时间区间
    setEffectiveTime(value) {
      console.log(value);
      if(value){
          this.form.effectiveTimeStart = value[0];
          this.form.effectiveTimeEnd = value[1];
      } else {
          this.form.effectiveTimeStart = null;
          this.form.effectiveTimeEnd = null;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scenicId: null,
        scenicName: null,
        scenicPhone: null,
        effectiveTimeStart: null,
        effectiveTimeEnd: null,
        scenicLabel: null,
        limitPurchaseNum: null,
        scenicStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        ticOtherId: null,
        scenicAuth: null
      };
      this.effectiveTime=null,
      this.dictScenicListSelected=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scenicId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 上架操作
    handleUpload(row){
      let _this=this
      this.$modal.confirm('是否确认上架该数据项？').then(function() {
        getOnshelf(row.scenicId).then(response => {
          console.log(response)
          _this.$modal.msgSuccess("上架成功");
          _this.getList()
        })
      }).catch(() => {});
      
    },
    // 下架操作
    handleDownload(row){
      let _this=this
      this.$modal.confirm('是否确认下架该数据项？').then(function() {
        getOffshelf(row.scenicId).then(response => {
          console.log(response)
          _this.$modal.msgSuccess("下架成功");
          _this.getList()
        })
      }).catch(() => {});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加景区门票基础";
      this.isEdit=true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scenicId = row.scenicId || this.ids
      getScenic(scenicId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改景区门票基础";
        let temp=[]
        temp.push(this.form.effectiveTimeStart)
        temp.push(this.form.effectiveTimeEnd)
        this.effectiveTime=temp
        // 设置默认选中景区其他信息
        this.changeScenicSelect(this.form.ticOtherId)
      });
      this.isEdit=false
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scenicId != null) {
            updateScenic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScenic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scenicIds = row.scenicId || this.ids;
      this.$modal.confirm('是否确认删除景区门票基础编号为"' + scenicIds + '"的数据项？').then(function() {
        return delScenic(scenicIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tictket/scenic/export', {
        ...this.queryParams
      }, `scenic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
